package com.example.demo.service;

import com.example.demo.dao.commodity;
import com.example.demo.dao.commodityRepository;
import com.example.demo.dao.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class commodityServiceImpl implements commodityService {

    @Autowired
    private commodityRepository commodityRepository;

    @Override
    public List<commodity> getCommodityByCommodityName(String commodityName) {
        List<commodity> commoditydb = commodityRepository.findByCommodityName(commodityName);
        return commoditydb;
    }

    @Override
    public List<commodity> getCommodityByUserName(String userName) {
        List<commodity> commodityList = commodityRepository.findByUserName(userName);
        System.out.println(commodityList);
        int listSize = commodityList.size();
        System.out.println("查询到" + listSize + "条数据");
        commodity commoditydb = commodityList.get(listSize-1);
        return commodityList;
    }

    @Override
    public List<commodity> getCommodityAll() {
        List<commodity> commodityList = commodityRepository.findAll();
        return commodityList;
    }

    @Override
    public commodity getCommodityById(long commodityId) {
        commodity commodity = commodityRepository.findByCommodityId(commodityId);
        return commodity;
    }

    @Override
    public commodity addCommodity(commodity commodity) {
        commodity commoditydb = commodityRepository.save(commodity);
        return commoditydb;
    }

    @Override
    public commodity updateCommoditySold(Long commodityId,int commodityOnSale, int commoditySold) {
        commodity commoditydb = commodityRepository.findByCommodityId(commodityId);
        commoditydb.setCommodityOnSale(commodityOnSale);
        commoditydb.setCommoditySold(commoditySold);
        commodity commodity = commodityRepository.save(commoditydb);
        return commodity;
    }

    @Override
    public void delcommodityById(long commodityId) {
        commodityRepository.deleteByCommodityId(commodityId);
    }


}
